<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      :header-cell-style="{ background: '#ffffff',color:'#000000' }"
      :key="tableKey"
      :row-class-name="tableRowClassName"
      style="width: 100%;font-size: 15px;box-shadow: 1px 1px 8px #cdcdcd;border: 0px;"
      @sort-change="sortChange"
    >
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="{ row }">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" label="Date">
        <template slot-scope="{ row }">
          <span>{{ row.timestamp | parseTime("{y}-{m}-{d} {h}:{i}") }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="Author">
        <template slot-scope="{ row }">
          <span>{{ row.author }}</span>
        </template>
      </el-table-column>

      <el-table-column width="100px" label="Importance">
        <template slot-scope="{ row }">
          <svg-icon
            v-for="n in +row.importance"
            :key="n"
            icon-class="star"
            class="meta-item__icon"
          />
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="Status" width="110">
        <template slot-scope="{ row }">
          <el-tag :type="row.status | statusFilter">
            {{ row.status }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column min-width="300px" label="Title">
        <template slot-scope="{ row }">
          <template v-if="row.edit">
            <el-input v-model="row.title" class="edit-input" size="small" />
            <el-button
              class="cancel-btn"
              size="small"
              icon="el-icon-refresh"
              type="warning"
              @click="cancelEdit(row)"
            >
              cancel
            </el-button>
          </template>
          <span v-else>{{ row.title }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Actions" width="120">
        <template slot-scope="{ row }">
          <el-button
            v-if="row.edit"
            type="success"
            size="small"
            icon="el-icon-circle-check-outline"
            @click="confirmEdit(row)"
          >
            Ok
          </el-button>
          <el-button
            v-else
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click="row.edit = !row.edit"
          >
            Edit
          </el-button>
        </template>
      </el-table-column>
    </el-table>

       <el-row>
      <el-col :span="24" class="page-tool">
        <el-pagination layout="total,sizes,prev, pager,next,jumper" :page-size="pageSize" :total="total" :page-sizes="[10, 20, 30, 40]" @size-change="handlePageChange" @current-change="handleNoChange"></el-pagination>
      </el-col>
    </el-row>


  </div>
</template>

<script>
import { fetchList } from "@/api/article";
import { loadeditApi } from "@/api/car";
import axios from 'axios'
import { getToken } from '@/utils/auth'
import waves from "@/directive/waves"; // waves directive
import { parseTime } from "@/utils";
import Pagination from "@/components/Pagination";

export default {
  name: "InlineEditTable",
    components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "info",
        deleted: "danger",
      };
      return statusMap[status];
    },
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      pageNo: 1,
      pageSize: 20,
      multipleSelection: [],
      total: 0,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        name: undefined,
        phone: undefined,
        number: undefined,
        sort: '+id'
      },
    };
  },
  created() {
       this.getList()

axios.get("/lclock?pageNo=1&pageSize=20&sort=%2Bid", {
          headers: {
            pacargle:getToken()
              // "eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2NTM0NzE3ODgsImV4cCI6MTY1MzQ3ODk4OCwibWFuZ2VySWQiOjEsIm5pY2tOYW1lIjoi6LaF57qn566h55CG5ZGYIiwicm9sZUlkIjoxLCJyZWZyZXNoQ291bnQiOjAsImxvZ2luTmFtZSI6ImFkbWluIn0.zGjWKwBIQOYQqor3sTjyA7mA-arOH3ZwRwq4jsb4WNA",
          },
        })




  },
  mounted: function () {

axios.get("/lclock?pageNo=1&pageSize=20&sort=%2Bid", {
          headers: {
            pacargle:getToken()
              // "eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2NTM0NzE3ODgsImV4cCI6MTY1MzQ3ODk4OCwibWFuZ2VySWQiOjEsIm5pY2tOYW1lIjoi6LaF57qn566h55CG5ZGYIiwicm9sZUlkIjoxLCJyZWZyZXNoQ291bnQiOjAsImxvZ2luTmFtZSI6ImFkbWluIn0.zGjWKwBIQOYQqor3sTjyA7mA-arOH3ZwRwq4jsb4WNA",
          },
        })


  },
  methods: {
    async getList() {
      this.listLoading = true;
      const { data } = await fetchList(this.listQuery);
      const items = data.items;
      this.list = items.map((v) => {
        this.$set(v, "edit", false); // https://vuejs.org/v2/guide/reactivity.html
        v.originalTitle = v.title; //  will be used when user click the cancel botton
        return v;
      });
      this.listLoading = false;
    },
    cancelEdit(row) {
      row.title = row.originalTitle;
      row.edit = false;
      this.$message({
        message: "The title has been restored to the original value",
        type: "warning",
      });
    },
    confirmEdit(row) {
      row.edit = false;
      row.originalTitle = row.title;
      this.$message({
        message: "The title has been edited",
        type: "success",
      });
    },
  },

  //列表页码的触发事件
  handlePageChange(pageSize) {
    this.pageSize = pageSize;
    this.loadedit(this.pageNo, this.pageSize);
  },
  handleNoChange(pageNo) {
    this.pageNo = pageNo;
    this.loadedit(this.pageNo, this.pageSize);
  },
  loadedit(pageNo, pageSize) {
    let _this = this;
    _this.loading = true;
    loadeditApi({ pageNo: pageNo, pageSize: pageSize }).then((response) => {
      _this.list = response.data.dataList;
      _this.total = response.data.totalNum;
      setTimeout(() => {
        _this.listLoading = false;
      }, 1 * 1000);
    });
  },
  //////////////////////////
};
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
